//
//  HotViewController.m
//  Travel_M_Travel
//
//  Created by Sun on 15/4/5.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HotViewController.h"
#import "RecommendHandler.h"
#import "AboutHotData.h"
#import "HotCViewCell.h"
#import "HPViewController.h"
#import "UIColor+AddColor.h"
@interface HotViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) NSMutableArray *hotArr;
@property (nonatomic, retain) UICollectionView *collectView;

@end

@implementation HotViewController

- (void)dealloc
{
    [_hotArr release];
    [_collectView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self dataHandler];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    
    flowLayout.minimumLineSpacing = 15;
    
    self.collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 115)collectionViewLayout:flowLayout];
    _collectView.dataSource = self;
    _collectView.delegate = self;
    _collectView.backgroundColor = [UIColor colorFromHexCode:@"#f6f5ec"];
    
    // 注册cell
    [_collectView registerClass:[HotCViewCell class] forCellWithReuseIdentifier:@"hotcell"];
    [self.view addSubview:_collectView];
    
    [flowLayout release];
    [_collectView release];
    
}

- (void)dataHandler
{
    NSString *urlStr = [[NSString stringWithFormat:@"http://api.breadtrip.com/trips/hot/?count=19&is_ipad=1&start=1"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [RecommendHandler getDataWithUrl:urlStr completion:^(NSDictionary *responseObject) {
       
        self.hotArr = [NSMutableArray array];
        NSMutableArray *tripArr = [responseObject objectForKey:@"trips"];
        for (NSMutableDictionary *dic in tripArr) {
            AboutHotData *hotData = [[AboutHotData alloc] initWhihDictionary:dic];
            
            [self.hotArr addObject:hotData];
            [hotData release];
            }
        [_collectView reloadData];
    }];

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    
    return CGSizeMake(self.view.bounds.size.width, 10);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.hotArr.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotCViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"hotcell" forIndexPath:indexPath];
    AboutHotData *hotData = [_hotArr objectAtIndex:indexPath.item];
    cell.aboutHot = hotData;
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(290, 160);
    
}

- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HPViewController *hpVC = [[HPViewController alloc] init];
    hpVC.hotData = self.hotArr[indexPath.item];
    [self.navigationController pushViewController:hpVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
